/*
 * Copyright (C) 2018 The Asiainfo-Ability Authors
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package org.asiainfo.ability.provider.custom;

import org.asiainfo.ability.base.service.TraceService;
import org.asiainfo.ability.provider.Context;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: visen
 * @Date: 2018/1/31
 * @Description:
 */
public class RequestContext extends ConcurrentHashMap<String, Object> {

    public final static String CALLEE_TRACE = "calleeTrace";
    public final static String RESPONSE_KEY = "responseKey";
    public static final String REQ_TIME = "X-Req-Time";
    public static final String RESP_TIME = "X-Resp-Time";
    public static final String TRACE_ID = "X-Trace-Id";
    public static final String BIZ_ID = "X-Biz-Id";
    public static final String SPAN_ID = "X-Span-Id";
    public static final String CONTEXT_KEY = "context";
    protected static Class<? extends RequestContext> contextClass = RequestContext.class;

    public RequestContext() {
        super();
    }

    public void set1() {

    }

    protected static final ThreadLocal<? extends RequestContext> threadLocal1 = new InheritableThreadLocal<RequestContext>() {
        @Override
        protected RequestContext initialValue() {
            try {
                return contextClass.newInstance();
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
        }
    };
    protected static final ThreadLocal<? extends RequestContext> threadLocal = new ThreadLocal<RequestContext>() {
        @Override
        protected RequestContext initialValue() {
            try {
                return contextClass.newInstance();
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
        }
    };


    public static RequestContext getCurrentContext() {
        RequestContext context = threadLocal.get();
        return context;
    }

    public static RequestContext getCurrentContext1() {
        RequestContext context = threadLocal1.get();
        return context;
    }
}
